/**
 * Created by 邵亚楠 on 2018/9/7.
 */
var sessionId = sessionStorage.getItem('sessionId');
$(function () {
    var id = getQueryString("id")
    //console.log(id)
    $.ajax({
        url: 'https://www.bjdeshangyuan.com/book/api/content/detail',
        type: 'POST',
        dataType: 'json',
        data: {'sessionid':sessionId, 'id': id},
        success: function (r) {
            console.log(r)
            if (r.code === 'USR000') {
                var newDate = r.data;
                var expandListData = r.data.expandList;
                var commentList = r.data.list
                var isLike = r.data.isLike
                if (isLike == 0) {
                    $('.browse_box .like_box .like').removeClass('bgc')
                } else {
                    $('.browse_box .like_box .like').addClass('bgc')
                }
                $('#title').text(newDate.title)
                $('.post_time').text(newDate.createTime)
                $('.content').html(newDate.text)
                $('.browse').text(newDate.browseTimes)
                $('.like_count').text(newDate.thumbUpTimes)

                $('.pic').css('background', 'url(http://www.bjdeshangyuan.com/book' + newDate.thumbnail + ')0% 0% / 100% 100%')
                //拓展阅读
                for (var i = 0; i < expandListData.length; i++) {
                    var newExpandList = expandListData[i]
                    var readId = newExpandList.id
                    $('.read_list').append('<a href="readDetail.html?id=' + readId + '">' + newExpandList.title + '</a>' + '</br>')
                }

                //评论循环
                for (var i = 0; i < commentList.length; i++) {
                    var id = commentList[i].commentId
                    //console.log(id)
                    var isLike = commentList[i].isLike
                    console.log(isLike)
                    //if (isLike == 0) {
                    //    $('.user_box .like_box .like').removeClass('bgc')
                    //} else {
                    //    $('.user_box .like_box .like').addClass('bgc')
                    //}
                    var icon_class_name = isLike == 0 ? '' : 'bgc';
                    var html = ""
                     html +=
                        '<div class="comment_item">' +
                        '<div class="pic">' +
                        '<img src="' + commentList[i].commentHeadImage + '" alt=""/>' +
                        '</div>' +
                        '<div class="comment-detail">' +
                        '<p class="user_box"><span>' + commentList[i].commentNiName + '</span><span class="like_box" onclick="getLike('+id+')" data-id="' + id + '"><span class="like '+icon_class_name+'"></span><span class="count">' + commentList[i].thumbUpTimes + '</span></span></p>' +
                        '<p class="text">' + commentList[i].commentText + '</p>' +
                        '</div>' +
                        '</div>'

                    $('.comment_list').prepend(html)

                }
            }

        }
    })
    //文章点赞
    $('.like_box').click(function () {
        //var sessionId = 'oQ_An0pr2htuLDV42lwE1LeRM220'
        //console.log(id)
        $.ajax({
            url: 'https://www.bjdeshangyuan.com/book/api/content/thumbUp',
            type: 'POST',
            dataType: 'json',
            data: {'sessionid': sessionId, 'contentId': id},
            success: function (r) {
                //console.log(r.data)
                if (r.code == "USR000") {
                    var likeCount = r.data.thumbUpTimes
                    var isLike = r.data.isLike
                    if (isLike == 0) {
                        $('.browse_box .like_box .like').removeClass('bgc')
                    } else {
                        $('.browse_box .like_box .like').addClass('bgc')
                    }

                    $('.like_count').text(likeCount)

                } else {
                    console.log('错误')
                }

            }
        })
    })

    //$('.like').click(function () {
    //    $(this).toggleClass('bgc')
    //})

$('.liuyan').click(function(){
    window.location.href = 'comment.html?id='+id+''+'&type=article'+''
})
})

//评论点赞
function getLike(id){
    //var sessionId = 'oQ_An0pr2htuLDV42lwE1LeRM220'
    $.ajax({
        url:'https://www.bjdeshangyuan.com/book/api/comment/thumbUp',
        type:'POST',
        data:{'sessionid':sessionId,'commentId':id},
        success:function(r){
            //console.log(r)
            if(r.code === 'USR000'){
                var Count = r.data.thumbUpTimes
                var isLike = r.data.isLike
                //$('.count').text(Count)
                $('.like_box').each(function(){
                    var $self = $(this);
                    //console.log($self)
                    if ($self.attr('data-id') == id) {
                        if (isLike == 0) {
                            $self.find('.like').removeClass('bgc');
                        } else {
                            $self.find('.like').addClass('bgc');
                        }
                        $self.find('.count').text(Count);
                    }
                })
            }
        }
    })

}



function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}


